package com.fimuz.mqmeet.api.team.service.impl;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.fimuz.mqmeet.api.team.dao.TeamDao;
import com.fimuz.mqmeet.api.team.service.TeamService;
import com.fimuz.mqmeet.api.team.service.model.CreateSpModel;
import com.fimuz.mqmeet.api.team.service.model.CreateSrModel;
import com.fimuz.mqmeet.api.team.service.model.GetSpModel;
import com.fimuz.mqmeet.api.team.service.model.GetSrModel;
import com.fimuz.mqmeet.helper.exception.MsResultCode;
import com.fimuz.mqmeet.helper.mybatis.dao.HxzLoginuserMapper;
import com.fimuz.mqmeet.helper.mybatis.dao.HxzTeamMapper;
import com.fimuz.mqmeet.helper.mybatis.model.HxzLoginuser;
import com.fimuz.mqmeet.helper.mybatis.model.HxzLoginuserExample;
import com.fimuz.mqmeet.helper.mybatis.model.HxzTeam;
import com.fimuz.mqmeet.helper.mybatis.model.HxzTeamExample;
import com.fimuz.mqmeet.helper.utils.PasswordUtils;
import com.fosun.spr.core.base.CoBaseService;
import com.fosun.spr.core.exception.ServiceException;
import com.fosun.spr.core.utils.ExtAsserts;
import com.fosun.spr.core.utils.SprDaoUtils;

@Transactional(rollbackFor = { RuntimeException.class, Exception.class })
@Service
public class TeamServiceImpl extends CoBaseService implements TeamService {
	@Resource
	private TeamDao teamDao;
	@Resource
	private HxzTeamMapper hxzTeamMapper;
	@Resource
	private HxzLoginuserMapper hxzLoginuserMapper;
 
	@Override
	public GetSrModel get(GetSpModel spModel) {
		GetSrModel srModel = new GetSrModel();
		return srModel;
	}	
	@Override
	public CreateSrModel create(CreateSpModel spModel) {
		ExtAsserts.notNull(spModel.getTeamName(),"teamName");
		ExtAsserts.notNull(spModel.getLoginPassword(),"loginPassword");
		ExtAsserts.notNull(spModel.getUserEmail(),"userEmail");
		ExtAsserts.notNull(spModel.getUserName(),"userName");
		CreateSrModel srModel = new CreateSrModel();
		
		//校验部门名是否已经存在
		if(true){			
			HxzTeamExample example = new HxzTeamExample();
			HxzTeamExample.Criteria c = example.createCriteria();
			c.andIsDeletedEqualTo(IS_DELETED_N);
			c.andTeamNameEqualTo(spModel.getTeamName());
			HxzTeam record = (HxzTeam)SprDaoUtils.getRowByExample(hxzTeamMapper, example);
			if(record!=null){
				throw new ServiceException(MsResultCode.MS10003); 
			}
		}
		//校验邮箱是否已经存在
		if(true){
			HxzLoginuserExample example = new HxzLoginuserExample();
			HxzLoginuserExample.Criteria c = example.createCriteria();
			c.andIsDeletedEqualTo(IS_DELETED_N);
			c.andUserEmailEqualTo(spModel.getUserEmail().toLowerCase());
			HxzLoginuser record = (HxzLoginuser)SprDaoUtils.getRowByExample(hxzLoginuserMapper, example);
			if(record!=null){
				throw new ServiceException(MsResultCode.MS10004);
			}	
		}
		HxzTeam team = new HxzTeam();
		team.setTeamName(spModel.getTeamName());
		team.setLinkmanEmail(spModel.getUserEmail());
		team.setLinkmanPhone(spModel.getUserPhone());
		team.setLinkmanName(spModel.getUserName());
		SprDaoUtils.insert(hxzTeamMapper, team);
		
		HxzLoginuser user = new HxzLoginuser();
		user.setActiveEmailFlag("0");
		user.setActivePhoneFlag("0");
		user.setLoginPassword(PasswordUtils.mi(spModel.getLoginPassword()));
		user.setTeamId(team.getId());
		user.setUserEmail(spModel.getUserEmail());
		user.setUserName(spModel.getUserName());
		user.setUserPhone(spModel.getUserPhone());
		SprDaoUtils.insert(hxzLoginuserMapper,user);
		
		team.setAdminId(user.getId());
		SprDaoUtils.updateByPrimaryKey(hxzTeamMapper, team);
		
		srModel.setLoginuserId(user.getId());
		srModel.setTeamId(team.getId());
		return srModel;
	}	
	
}
